package com.iris.live.services.data.repositories;

import com.iris.live.services.data.models.PushApprovalWorkFlowModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public interface PushApprovalWorkFlowRepository2 extends JpaRepository<PushApprovalWorkFlowModel, Integer>,JpaSpecificationExecutor<PushApprovalWorkFlowModel>{
    @Query("select a from PushApprovalWorkFlowModel a where a.type=?1 and a.permit=?2 and a.quotedPriceCodeOrVin=?3 order by a.processTime desc")
    List<PushApprovalWorkFlowModel> findByPermitAndTypeAndVin(Integer Type, String permit, String quotedPriceCodeOrVin);

    @Query("select a from PushApprovalWorkFlowModel a where a.transcationCode = ?1")
    List<PushApprovalWorkFlowModel> getModelByTransactionCode(String transactionCode);

    @Query("select a from PushApprovalWorkFlowModel a where a.quotedPriceCodeOrVin=?1")
    List<PushApprovalWorkFlowModel> getFlowByQuo(String quo);

    @Query("select a from PushApprovalWorkFlowModel a where a.quotedPriceCodeOrVin=?1 order by a.id desc ")
    List<PushApprovalWorkFlowModel> getFlowByQuoOrderById(String quo);

    @Modifying
    @Query("delete  FROM PushApprovalWorkFlowModel u where u.quotedPriceCodeOrVin =?1")
    void deleteByQuoId(String vin);

    @Query("select a from PushApprovalWorkFlowModel a where a.permit = ?1 and a.type = ?2 and a.quotedPriceCodeOrVin = ?3")
    List<PushApprovalWorkFlowModel> getWorkFlowByPermitTypeId(String permit, Integer type, String id);

    /**
     * 根据初始报价单号，查找单子的最新审批记录
     * @param initNum
     * @return
     */
    @Query(value = "select a.* from pushapprovalworkflow a where a.quotedPriceCodeOrVIN in(select 报价编号 from 报价单表 where 初始报价单号 = ?1) order by id desc limit 1",nativeQuery = true)
    PushApprovalWorkFlowModel getTheLastOneByInitNum(String initNum);

}
